#include <webx/menu.h>
#include <dbentity/T_XG_CONF.h>

class GetNoteContent : public webx::ProcessBase
{
protected:
	int process();
};

HTTP_WEBAPP(GetNoteContent, "/confile/${filename}")

int GetNoteContent::process()
{
	param_string(id);

	webx::CheckAlnumString(id);
	
	checkLogin();

	string sqlcmd;
	sp<DBConnect> dbconn = webx::GetDBConnect(dbid);
	
	stdx::format(sqlcmd, "SELECT ID,USER,ICON,LEVEL,TITLE,FOLDER,CONTENT,STATETIME FROM T_XG_CONF WHERE (USER='%s' OR LEVEL>2) AND ID='%s'", user.c_str(), id.c_str());

	sp<QueryResult> rs = dbconn->query(sqlcmd);

	if (!rs) return simpleResponse(XG_SYSERR);

	sp<RowData> row = rs->next();

	if (!row) return simpleResponse(XG_NOTFOUND);

	string author = row->getString(1);

	json["id"] = row->getString(0);
	json["icon"] = row->getString(2);
	json["level"] = row->getString(3);
	json["title"] = row->getString(4);
	json["folder"] = row->getString(5);
	json["content"] = row->getString(6);
	json["statetime"] = row->getString(7);

	if (user == author)
	{
		CT_XG_CONF tab;

		tab.init(dbconn);

		tab.id = id;
		tab.statetime.update();

		tab.update();
	}

	json["code"] = XG_OK;
	out << json;

	return XG_OK;
}